package org.database.config;

import com.mybatisflex.core.FlexGlobalConfig;
import com.mybatisflex.core.audit.AuditManager;
import com.mybatisflex.spring.boot.MyBatisFlexCustomizer;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.database.attribute.FlexLoggingAttribute;
import org.springframework.context.annotation.Configuration;

/**
 * MyBatisFlexConfiguration
 *
 * @author grl
 * @date 2024/1/11
 */
@Slf4j
@Configuration
@RequiredArgsConstructor
public class MyBatisFlexConfiguration implements MyBatisFlexCustomizer {

    private final FlexLoggingAttribute loggingAttribute;
    @Override
    public void customize(FlexGlobalConfig flexGlobalConfig) {
        if(loggingAttribute.isAudit()){
            //开启审计功能
            AuditManager.setAuditEnable(true);
            //设置 SQL 审计收集器
            AuditManager.setMessageCollector(auditMessage ->
                    log.info("{},{}ms", auditMessage.getFullSql()
                            , auditMessage.getElapsedTime())
            );
        }

    }
}
